/**
 * 
 */
package COMM232;

import java.util.Enumeration;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

/**
 * Classe que vai encapsular os parametros necessarios a configuracao serial.
 * 
 * @author Emerson Beserra
 *
 */
public class SerialParameters 
{
	static final int FLOWCONTROL_NONE        = SerialPort.FLOWCONTROL_NONE; 
    
	static final int FLOWCONTROL_RTSCTS_IN   = SerialPort.FLOWCONTROL_RTSCTS_IN;
	            
	static final int FLOWCONTROL_RTSCTS_OUT  = SerialPort.FLOWCONTROL_RTSCTS_OUT; 
	            
	static final int FLOWCONTROL_XONXOFF_IN  = SerialPort.FLOWCONTROL_XONXOFF_IN; 
	            
	static final int FLOWCONTROL_XONXOFF_OUT = SerialPort.FLOWCONTROL_XONXOFF_OUT; 
	
	static final int DATABITS_5 = SerialPort.DATABITS_5;
	
	static final int DATABITS_6 = SerialPort.DATABITS_6;
	
	static final int DATABITS_7 = SerialPort.DATABITS_7;
	
	static final int DATABITS_8 = SerialPort.DATABITS_8;
	
	static final int BAUD_9600    = 9600;
	
	static final int BAUD_19200   = 19200;
	
	static final int BAUD_57600   = 57600;
	
	static final int BAUD_115200  = 115200;
	
	static final int PARITY_EVEN    = SerialPort.PARITY_EVEN; 
    
	static final int PARITY_MARK    = SerialPort.PARITY_MARK; 
	            
	static final int PARITY_NONE    = SerialPort.PARITY_NONE; 
	            
	static final int PARITY_ODD     = SerialPort.PARITY_ODD; 
	            
	static final int PARITY_SPACE   = SerialPort.PARITY_SPACE; 
	            
	static final int STOPBITS_1   = SerialPort.STOPBITS_1; 
	            
	static final int STOPBITS_1_5 = SerialPort.STOPBITS_1_5; 
	            
	static final int STOPBITS_2   = SerialPort.STOPBITS_2; 
	
	//atributos privados da classe
	
	private int dataBits;
	
	private int flowControl;
	
	private int parity;
	
	private int stopBits;
	
	private int baudRate;
	
	private CommPortIdentifier com;
	
	/**
	 * Lista de identificadores de portas (seriais ou paralelas).
	 */
	private CommPortIdentifier[] comID;
	
//============================================================================\\
//========================DECLARACAO DE CONSTRUTORES==========================\\
//============================================================================\\
	
	
	/**
	 * Construtor que vai inicializar os atributos da porta serial de forma 
	 * basica. (9600, 8, N, 1)
	 * Vai optar por abrir a primeira porta serial encontrada no sistema.
	 */
	public SerialParameters()
	{
		initialize();
	}//end SerialParameters()---------------------------------------------------
	
	/**
	 * Construtor que vai inicializar a porta especificada atraves do parametro 
	 * com.
	 * @param com
	 * @throws ParametersException
	 */
	public SerialParameters(String str) throws ParametersException
	{
		initialize();
		setCom(str);
	}
	
	/**
	 * Construtor que vai inicializar a porta especificada com o baud rate
	 * especificado.
	 * @param com
	 * @param baud
	 * @throws ParametersException
	 */
	public SerialParameters(String com, int baud) throws ParametersException
	{
		initialize();
		setCom(com);
		setBaudRate(baud);
	}

//============================================================================\\
//====================FIM DE DECLARACAO DE CONSTRUTORES=======================\\
//============================================================================\\

	/**
	 * 
	 */
	private void setCom(String str) throws ParametersException
	{
		int aux = 0;
		boolean find = false;
		while( ( comID.length > aux ) )
		{
			if( comID[aux] == null )
				break;
			if( comID[aux].getName().equals(str) )
			{
				find = true;
				break;
			}
			aux++;
		}//end while
		if( !find )
			throw new ParametersException("Error at COM port name.");
		else
			com = comID[aux];
	}
	
	/**
	 * Inicializao basica dos atributos.
	 */
	private void initialize()
	{
		dataBits = SerialPort.DATABITS_8;
		flowControl = SerialPort.FLOWCONTROL_NONE;
		parity = SerialPort.PARITY_NONE;
		stopBits = SerialPort.STOPBITS_1;
		baudRate = SerialParameters.BAUD_9600;
		
		Enumeration enumeraPortas = CommPortIdentifier.getPortIdentifiers(); //Retorna um objeto Enumeration que contem um objeto CommPortIndentifier para cada porta no sistema.
		comID = new CommPortIdentifier[30]; //Numero arbitrario de portas que possam existir no sistema.
		
		CommPortIdentifier portaId;
		int aux = 0;
		
		for(int i = 0; i < 30; i++)
			comID[i] = null;
		
		while (enumeraPortas.hasMoreElements()) 
		{
			portaId = (CommPortIdentifier) enumeraPortas.nextElement(); //Percorre as portas no objeto Enumeration enumerador.
			if (portaId.getPortType() == CommPortIdentifier.PORT_SERIAL) //Verifica se eh uma porta serial.
				comID[aux++] = portaId; //Insere a porta serial no array identificador de portas.
		}//end while		
		
		com = comID[0];
		
//		for(int k = 0; k < comID.length; k++)
//			System.out.println(comID[k].getName());
	}//end initialize()---------------------------------------------------------
	
	/**
	 * 
	 * @param bits
	 * @return
	 * @throws ParametersException
	 */
	public int setDataBits(int bits) throws ParametersException
	{
		switch (bits) 
		{
			case SerialParameters.DATABITS_5:
				return bits;
			case SerialParameters.DATABITS_6:
				return bits;
			case SerialParameters.DATABITS_7:
				return bits;
			case SerialParameters.DATABITS_8:
				return bits;
			default:
				throw new ParametersException("Error at number of data bits.");
		}//end switch
	}//end setDataBits()--------------------------------------------------------
	
	/**
	 * 
	 * @param data
	 * @return
	 * @throws ParametersException
	 */
	public int setFlowControl(int data) throws ParametersException
	{
		switch (data) 
		{
			case SerialParameters.FLOWCONTROL_NONE:
				return SerialPort.FLOWCONTROL_NONE;
			case SerialParameters.FLOWCONTROL_RTSCTS_IN:
				return SerialPort.FLOWCONTROL_RTSCTS_IN;
			case SerialParameters.FLOWCONTROL_RTSCTS_OUT:
				return SerialPort.FLOWCONTROL_RTSCTS_OUT;
			case SerialParameters.FLOWCONTROL_XONXOFF_IN:
				return SerialPort.FLOWCONTROL_XONXOFF_IN;
			case SerialParameters.FLOWCONTROL_XONXOFF_OUT:
				return SerialPort.FLOWCONTROL_XONXOFF_OUT;
			default:
				throw new ParametersException("Error at type of flow control.");
		}//end switch
	}//end setFlowControl()-----------------------------------------------------
	
	/**
	 * 
	 * @param baud
	 * @return
	 * @throws ParametersException
	 */
	public int setBaudRate(int baud) throws ParametersException
	{
		switch (baud) 
		{
			case SerialParameters.BAUD_115200:
				return baud;
			case SerialParameters.BAUD_19200:
				return baud;
			case SerialParameters.BAUD_57600:
				return baud;
			case SerialParameters.BAUD_9600:
				return baud;
			default:
				throw new ParametersException("Error at baud rate value. Choose another value.");
		}//end switch
	}//end setBaudRate()--------------------------------------------------------
	
	/**
	 * 
	 * @param par
	 * @return
	 * @throws ParametersException
	 */
	public int setParity(int par) throws ParametersException
	{
		switch (par) 
		{
			case SerialParameters.PARITY_EVEN:
				return SerialPort.PARITY_EVEN;
			case SerialParameters.PARITY_MARK:
				return SerialPort.PARITY_MARK;
			case SerialParameters.PARITY_NONE:
				return SerialPort.PARITY_NONE;
			case SerialParameters.PARITY_ODD:
				return SerialPort.PARITY_ODD;
			case SerialParameters.PARITY_SPACE:
				return SerialPort.PARITY_SPACE;
			default:
				throw new ParametersException("Error at type of parity.");
		}//end switch
	}//end setParity()----------------------------------------------------------
	       
	
	public int setStopBits(int stop) throws ParametersException
	{
		switch (stop) 
		{
			case SerialParameters.STOPBITS_1:
				return SerialPort.STOPBITS_1;
			case SerialParameters.STOPBITS_1_5:
				return SerialPort.STOPBITS_1_5;
			case SerialParameters.STOPBITS_2:
				return SerialPort.STOPBITS_2;
			default:
				throw new ParametersException("Error at number fo stop bits.");
		}//end switch
	}

	/**
	 * @return the fLOWCONTROL_NONE
	 */
	public static int getFLOWCONTROL_NONE()
	{
		return FLOWCONTROL_NONE;
	}

	/**
	 * @return the fLOWCONTROL_RTSCTS_IN
	 */
	public static int getFLOWCONTROL_RTSCTS_IN()
	{
		return FLOWCONTROL_RTSCTS_IN;
	}

	/**
	 * @return the fLOWCONTROL_RTSCTS_OUT
	 */
	public static int getFLOWCONTROL_RTSCTS_OUT()
	{
		return FLOWCONTROL_RTSCTS_OUT;
	}

	/**
	 * @return the fLOWCONTROL_XONXOFF_IN
	 */
	public static int getFLOWCONTROL_XONXOFF_IN()
	{
		return FLOWCONTROL_XONXOFF_IN;
	}

	/**
	 * @return the fLOWCONTROL_XONXOFF_OUT
	 */
	public static int getFLOWCONTROL_XONXOFF_OUT()
	{
		return FLOWCONTROL_XONXOFF_OUT;
	}

	/**
	 * @return the dATABITS_5
	 */
	public static int getDATABITS_5()
	{
		return DATABITS_5;
	}

	/**
	 * @return the dATABITS_6
	 */
	public static int getDATABITS_6()
	{
		return DATABITS_6;
	}

	/**
	 * @return the dATABITS_7
	 */
	public static int getDATABITS_7()
	{
		return DATABITS_7;
	}

	/**
	 * @return the dATABITS_8
	 */
	public static int getDATABITS_8()
	{
		return DATABITS_8;
	}

	/**
	 * @return the bAUD_9600
	 */
	public static int getBAUD_9600()
	{
		return BAUD_9600;
	}

	/**
	 * @return the bAUD_19200
	 */
	public static int getBAUD_19200()
	{
		return BAUD_19200;
	}

	/**
	 * @return the bAUD_57600
	 */
	public static int getBAUD_57600()
	{
		return BAUD_57600;
	}

	/**
	 * @return the bAUD_115200
	 */
	public static int getBAUD_115200()
	{
		return BAUD_115200;
	}

	/**
	 * @return the pARITY_EVEN
	 */
	public static int getPARITY_EVEN()
	{
		return PARITY_EVEN;
	}

	/**
	 * @return the pARITY_MARK
	 */
	public static int getPARITY_MARK()
	{
		return PARITY_MARK;
	}

	/**
	 * @return the pARITY_NONE
	 */
	public static int getPARITY_NONE()
	{
		return PARITY_NONE;
	}

	/**
	 * @return the pARITY_ODD
	 */
	public static int getPARITY_ODD()
	{
		return PARITY_ODD;
	}

	/**
	 * @return the pARITY_SPACE
	 */
	public static int getPARITY_SPACE()
	{
		return PARITY_SPACE;
	}

	/**
	 * @return the sTOPBITS_1
	 */
	public static int getSTOPBITS_1()
	{
		return STOPBITS_1;
	}

	/**
	 * @return the sTOPBITS_1_5
	 */
	public static int getSTOPBITS_1_5()
	{
		return STOPBITS_1_5;
	}

	/**
	 * @return the sTOPBITS_2
	 */
	public static int getSTOPBITS_2()
	{
		return STOPBITS_2;
	}

	/**
	 * @return the dataBits
	 */
	public int getDataBits()
	{
		return dataBits;
	}

	/**
	 * @return the flowControl
	 */
	public int getFlowControl()
	{
		return flowControl;
	}

	/**
	 * @return the parity
	 */
	public int getParity()
	{
		return parity;
	}

	/**
	 * @return the stopBits
	 */
	public int getStopBits()
	{
		return stopBits;
	}

	/**
	 * @return the baudRate
	 */
	public int getBaudRate()
	{
		return baudRate;
	}

	/**
	 * @return the com
	 */
	public CommPortIdentifier getCom()
	{
		return com;
	}

	/**
	 * @return the comID
	 */
	public CommPortIdentifier[] getComID()
	{
		return comID;
	}

}
